package com.yuanheng100.ems4j.service;

import com.alibaba.fastjson.JSON;
import com.yuanheng100.ems4j.api.IEmployeeService;
import com.yuanheng100.ems4j.mapper.Ems4jDepartmentMapper;
import com.yuanheng100.ems4j.mapper.Ems4jEmployeeMapper;
import com.yuanheng100.ems4j.model.Ems4jDepartment;
import com.yuanheng100.ems4j.model.Ems4jEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jlqian on 2017/1/11.
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService
{
    private static Logger logger = LoggerFactory.getLogger(EmployeeServiceImpl.class);

    @Autowired
    private Ems4jEmployeeMapper ems4jEmployeeMapper;
    @Autowired
    private Ems4jDepartmentMapper ems4jDepartmentMapper;

    @Override
    public Ems4jEmployee getEmployeeById(int userId)
    {
        return ems4jEmployeeMapper.selectByPrimaryKey(userId);
    }

    @Override
    public Ems4jEmployee getEmployeeByMobile(Long mobile)
    {
        return ems4jEmployeeMapper.selectByMobile(mobile);
    }

    @Override
    public Ems4jEmployee getEmployeeByEmail(String email)
    {
        return ems4jEmployeeMapper.selectByEmail(email);
    }

    @Override
    public List<Ems4jEmployee> getEmployeeListByDepId(int depId, int layer, int appId)
    {
        return getEmployeeListByDepIdRoleIdStatus(depId, layer, 0, (short) 0, appId);
    }

    @Override
    public List<Ems4jEmployee> getEmployeeListByDepIdRoleIdStatus(int depId, int layer, int roleId, short status, int appId)
    {
        if (layer == 0)
        {
            layer = Integer.MAX_VALUE;
        }
        return getRecursionEmployeeListByDepIdRoleIdStatus(depId, layer, roleId, status, appId, 1);
    }


    public List<Ems4jEmployee> getRecursionEmployeeListByDepIdRoleIdStatus(int depId, int layer, int roleId, short status, int appId, int currentLayer)
    {
        //符合要求的员工
        List<Ems4jEmployee> expectedEmployees = new ArrayList<>();
        //获取当前层次员工
        List<Ems4jEmployee> ems4jEmployees = ems4jEmployeeMapper.selectEmployeeListByDepIdRoleIdStatus(depId, roleId, status);
        for (Ems4jEmployee ems4jEmployee : ems4jEmployees)
        {
            String appids = ems4jEmployee.getAppids();
            List<Integer> list = JSON.parseArray(appids, Integer.class);
            if (list != null && list.contains(appId))
            {
                expectedEmployees.add(ems4jEmployee);
            }
        }
        //获取子部门中的员工
        List<Ems4jDepartment> ems4jDepartments = ems4jDepartmentMapper.selectByParentid(depId);
        for (Ems4jDepartment ems4jDepartment : ems4jDepartments)
        {
            currentLayer++;
            if (currentLayer <= layer)
            {
                ems4jEmployees.addAll(getRecursionEmployeeListByDepIdRoleIdStatus(ems4jDepartment.getId(), layer, roleId, status, appId, currentLayer));
            }
        }
        return expectedEmployees;
    }


    @Override
    public int addEmployee(Ems4jEmployee employee)
    {
        logger.info("新增员工：" + JSON.toJSONString(employee));
        return ems4jEmployeeMapper.insert(employee);
    }

    @Override
    public int updateEmployee(Ems4jEmployee employee)
    {
        logger.info("修改员工：" + JSON.toJSONString(employee));
        return ems4jEmployeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public List<Ems4jEmployee> getAllEmployees()
    {
        return ems4jEmployeeMapper.selectAllEmployees();
    }

    @Override
    public int transferEmployee(Integer fromDepartmentId, Integer toDepartmentId)
    {
        return ems4jEmployeeMapper.transferEmployee(fromDepartmentId, toDepartmentId);
    }
}
